/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.training.controller;


import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.domain.DataBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.safetytask.utils.BusinessCode;
import com.koron.training.domain.PersonnalCertification;
import com.koron.training.service.api.MatrixService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "matrix", description = "教育培训-证照矩阵、人员持证、培训地图接口")
@RestController
@RequestMapping("/matrix")
public class MatrixController {
    private final static Logger logger= LoggerFactory.getLogger(MatrixController.class);
    @Autowired
    private MatrixService service;

    @ApiOperation("证照矩阵,(0内授权1外授权，3没有证照)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "岗位名称" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页" , dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , dataType = "int", paramType = "query"),
    })
    @RequestMapping(value = "/queryDutyCetificate.htm", method = RequestMethod.GET)
    public MessageBean<?> queryDutyCetificate(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam final String corpCode, @RequestParam(required = false) final String name,
                                              @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer pageSize) {
        if (null == user) {
            MessageBean<Integer> messageBean = new MessageBean<>();
            messageBean.setCode(BusinessCode.NOT_LOGIN.getCode());
            messageBean.setDescription(BusinessCode.NOT_LOGIN.getDescription());
            return messageBean;
        }
        Integer start = null;
        if (page != null && page >= 0) {
            if (pageSize == null || pageSize <= 0) {
                pageSize = 15;
            }
            start = (page <= 1) ? 0 : (page - 1) * pageSize;
        }
        return service.queryDutyCetificate(null, corpCode, name, start, pageSize);
    }

    @ApiOperation("导出证照矩阵,(0内授权1外授权，3没有证照)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "岗位名称" , dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/exportDutyCetificate.htm", method = RequestMethod.GET)
    public HttpEntity<?> exportDutyCetificate(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam final String corpCode, @RequestParam(required = false) final String name) {
        if (null == user) {
            return null;
        }
        MessageBean<Map> msg = service.exportDutyCetificate(corpCode, name, null);
        File targetFile = new File("static/excel/dutyCetificate.xlsx");
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/dutyCetificate.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("证照矩阵", targetFile, "dutyCetificate.btl", msg.getData());
    }

    @ApiOperation("人员持证矩阵")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "staffName", value = "人员姓名" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "dutyName", value = "岗位工种" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "certificateName", value = "证照名称" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "deptCode", value = "人员所在单位code" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态,0有效，1过期，2丢失" , dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页" , dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , dataType = "int", paramType = "query"),
    })
    @RequestMapping(value = "/queryPersonalCertification.htm", method = RequestMethod.GET)
    public MessageBean<DataBean<PersonnalCertification>> queryPersonalCertification(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam final String corpCode, @RequestParam(required = false) final String staffName, @RequestParam(required = false) final String dutyName,
                                                                                    @RequestParam(required = false) final String certificateName, @RequestParam(required = false) final String deptCode,
                                                                                    @RequestParam(required = false) final Integer status, @RequestParam Integer page, @RequestParam Integer pageSize) {
        if (null == user) {
            MessageBean<DataBean<PersonnalCertification>> messageBean = new MessageBean<>();
            messageBean.setCode(BusinessCode.NOT_LOGIN.getCode());
            messageBean.setDescription(BusinessCode.NOT_LOGIN.getDescription());
            return messageBean;
        }
        Integer start = null;
        if (page == null || page <= 0) {
            if (pageSize == null || pageSize <= 0) {
                pageSize = 15;
            }
        }
        start = (page <= 1) ? 0 : (page - 1) * pageSize;
        return service.queryPersonalCertification(corpCode, staffName, dutyName, certificateName, deptCode, status, start, pageSize);
    }

    @ApiOperation("导出人员持证矩阵")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "staffName", value = "人员姓名" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "dutyName", value = "岗位工种" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "certificateName", value = "证照名称" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "deptCode", value = "人员所在单位code" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态" , dataType = "int", paramType = "query"),
    })
    @RequestMapping(value = "/exportPersonalCertification.htm", method = RequestMethod.GET)
    public HttpEntity<?> exportPersonalCertification(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam final String corpCode, @RequestParam(required = false) final String staffName, @RequestParam(required = false) final String dutyName,
                                                     @RequestParam(required = false) final String certificateName, @RequestParam(required = false) final String deptCode,
                                                     @RequestParam(required = false) final Integer status) {
        if (null == user) {
            return null;
        }
        MessageBean<List<PersonnalCertification>> msg = service.exportPersonalCertification(corpCode, staffName, dutyName, certificateName, deptCode, status);
        File targetFile = new File("static/excel/personalCertificationMatrix.xlsx");
        Map<String, Object> map = new HashMap<>();
        map.put("data", msg.getData());
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/personalCertificationMatrix.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("人员持证", targetFile, "personalCertificationMatrix.btl", map);
    }
}
